package util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class MYUtil {

	public static Boolean SHOW_LOG = true;// 是否显示控制台打印
	public static Boolean SHOW_SINGLE = true;// 显示
	public static Boolean NOT_SHOW_SINGLE = true;// 不显示
	/**
	 * MD5加密 96e79218965eb72c92a549dd5a330112
	 * 
	 * @param str
	 * @return
	 */
	public static String Md5Str(String str) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			MYUtil.printMyLog("NoSuchAlgorithmException caught!",SHOW_SINGLE);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return md5StrBuff.toString();
	}

	/**
	 * 获取request
	 * 
	 * @return
	 */
	public static HttpServletRequest getMyRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 获取session
	 * 
	 * @return
	 */
	public static HttpSession getMySession() {
		return getMyRequest().getSession();
	}

	/**
	 * 格式化时间
	 * 
	 * @param sdate
	 * @param format
	 * @return
	 */
	public static String dateFormat(Date date, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 获取客户端操作系统
	 * 
	 * @param agent
	 * @return
	 */
	public static String getClientOS(String agent) {
		String os = "未知";
		agent = agent.toLowerCase();
		if (agent.indexOf("windows nt 5.1") > 0) {
			os = "winxp";
		} else if (agent.indexOf("windows nt 6.1") > 0) {
			os = "win7";
		} else if (agent.indexOf("windows nt 6.2") > 0) {
			os = "win8";
		} else if (agent.indexOf("windows nt 10.0") > 0) {
			os = "win10";
		}
		return os;
	}

	/**
	 * 获取客户端浏览器类型
	 * 
	 * @param agent
	 * @return
	 */
	public static String getClientBrowser(String agent) {
		String browser = "未知";
		agent = agent.toLowerCase();
		if (agent.indexOf("edge") > 0) {
			browser = "Edge";
		} else if (agent.indexOf("firefox") > 0) {
			browser = "Firefox";
		} else if (agent.indexOf("chrome") > 0) {
			browser = "Chrome";
		} else if (agent.indexOf("opera") > 0) {
			browser = "Opera";
		} else if (agent.indexOf("msie 7.0") > 0) {
			browser = "IE7";
		} else if (agent.indexOf("msie 8.0") > 0) {
			browser = "IE8";
		} else if (agent.indexOf("msie 9.0") > 0) {
			browser = "IE9";
		} else if (agent.indexOf("msie 10.0") > 0) {
			browser = "IE10";
		}
		return browser;
	}

	/**
	 * 自定义控制台显示
	 * 
	 * @param msg
	 */
	public static void printMyLog(String msg, Boolean flag) {
		if (SHOW_LOG && flag) {
			System.out.println(msg);
		}
	}
}
